package com.challenge.Shelf;

import com.challenge.Order.Order;
import com.challenge.util.FormateString;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 *  used for sort
 */
public class SortShelf {

    public ArrayList<Order> store = new ArrayList<Order>();

    public void sort(){
        Collections.sort(store,new Comparator<Order>() {
            @Override
            public int compare(Order o1, Order o2) {
                // TODO Auto-generated method stub
                return o1.getLeftLifeTime()-o2.getLeftLifeTime();
            }
        });
    }

    /**
     * record all order's shelfLife
     */
    public void shelfLifeCompute(){
        SortShelf sortShelf1 = new SortShelf();
        for(int i=0;i<this.store.size();i++){
            Order order = this.store.get(i);

            int sec = order.orderValueCompute();
            order.setLeftLifeTime(sec);
            double nowLeftTime = sec - order.getOrderAge();//left shelfLife sec time
            System.out.println(" NO." + (i+1) + "  " + FormateString.padRight(order.getName(),25," ")  + " order's left shelfLife is:"+ sec + " ; initCostTime :" + order.getOrderAge()  + " nowLeftTime is :" + nowLeftTime);
            order.setLeftLifeTime((int)Math.ceil(nowLeftTime));
            //Match the time of the above order to calculate the current remaining order shelfLife
            sortShelf1.store.add(order);
        }

        this.store.clear();
        this.store.addAll(sortShelf1.store);
    }
}
